/**
 * 系统消息
 * @author truexin
 * @date   2016/07/19
 */

import PullRefresh from '../components/mui/PullRefresh.js';
import NoData from '../components/NoData.js';

(function (lf, r) {
    
    var ajax = lf.utils.ajax,
        fmt = 'yyyy-MM-dd hh:mm',
        doAction = lf.bridgeJs.doAction,
        houseNum = '',
        RC = r.createClass;
    
    var System = RC({
        componentDidMount: function () {
            var toDetail = r.findDOMNode(this.refs.system);
            var me = this;
            var data = me.props.data,
                user = me.props.user,
                userId = data.userId,
                businessType = data.businessType,
                businessId = businessType == '018' ? data.id : data.businessId; //(feeNoticeDetailId)
            
            var match = data.title.search('物业须知');
            
            let api = match < 0 ? lf.finger.api.property.getNotice : lf.finger.api.property.getProjectNotice;
            
            $(toDetail).on('tap', function () {
                
                if (businessId && businessType != '018') {
                    
                    let url = match < 0 ?
                    'property-notice-intro.html?noticeId=' + businessId :
                    'property-instructions-detail.html?id=' + businessId;
                    
                    ajax({
                        type: "get",
                        data: {
                            noticeId: businessId,
                            id: businessId
                        },
                        success: function (data) {
                            
                            let title = data.data.title;
                            me.browserRecord(user, businessId);
                            me.alreadyRead(url, title, businessId, userId);
                            
                        }.bind(me)
                    }, api)
                } else {
                    if (businessType == '018') {
                        
                        let title = '费用查询',
                            url = 'property-fee-query.html?feeNoticeDetailId=' + businessId;
                        me.browserRecord(user, businessId);
                        me.alreadyRead(url, title, businessId, userId);
                        
                    }
                }
                
            })
            
            
        },
        browserRecord: function (user, businessId) {
            ajax({
                type: "POST",
                data: {
                    userType: user.householdType,
                    userId: user.id,
                    noticeId: businessId
                },
                success: function () {
                    return
                }
            }, lf.finger.api.property.saveNoticeBrowse);
        },
        alreadyRead: function (url, title, businessId, userId) {
            ajax({
                type: "post",
                data: {
                    userId: userId,
                    businessId: businessId,
                },
                success: function () {
                    doAction(url, title);
                }
            }, lf.finger.api.message.updateById)
        },
        render: function () {
            var data = this.props.data,
                createTime = lf.utils.format(data.createTime, fmt);
            var tips = '',
                blue = '';
            if (data.status == '1') {
                tips = '未查看';
            } else if (data.status == '2') {
                tips = '已查看';
                blue = 'blue';
            } else {
                tips = '已删除';
            }
            return (
                <div className='system-content' ref="system" onTouchEnd={this.clickFn}>
                    <span className='lf sys'>
                         <i>系统</i>
                    </span>
                    <b>{createTime}</b>
                    <span className="head-img mui-icon mui-icon-email">
                    </span>
                    <p className="content text-dot"> {data.title}</p>
                    <a href="#" className={"detail " + blue}>{tips}</a>
                </div>
            )
        }
    });
    
    var Body = RC({
        getInitialState: function () {
            return ({
                list: [],
                itemList: []
            })
        },
        componentWillMount: function () {
            var me = this;
            var page = this.props.page,
                user = this.props.user;
            me.getHouseNum(user, page);
            
            lf.appJs.refresh();
        },
        getHouseNum: function (user, page) {
            var me = this;
            if (user.houseId) {
                ajax({
                    type: 'get',
                    data: {
                        houseId: user.houseId,
                    },
                    success: function (data) {
                        houseNum = data.data.houseNum;
                        me.getList(user, houseNum, page);
                    }.bind(me)
                }, lf.finger.api.household.getHouseInfo);
            } else {
                ajax({
                    type: 'get',
                    data: {
                        globalId: user.globalId,
                    },
                    success: function (data) {
                        var houseNumList = [];
                        $.each(data.dataList, function () {
                            if (this.type == 1) {
                                houseNumList.push(this.houseNum);
                            }
                        });
                        houseNum = houseNumList[0];
                        me.getList(user, houseNum, page);
                    }.bind(me)
                }, lf.finger.api.household.getHouseList);
            }
        },
        getList: function (user, houseNum, page) {
            if (!page) page = 1;
            var me = this;
            ajax({
                type: 'post',
                data: {
                    userId: user.userId,
                    projectId: user.projectId,
                    houseNum: houseNum,
                    page: page,
                    // pageSize: 7
                },
                success: function (data) {
                    if (data.code == '200') {
                        var noData = {
                            span_text: '暂无消息',
                        };
                        if (!data.dataList.length) {
                            var list = (function () {
                                return <NoData data={noData}/>
                            })()
                        }
                        else {
                            let itemList = me.state.itemList;
                            let resultList = data.dataList;
                            
                            
                            $.each(resultList, function (i, item) {
                                itemList.push(item);
                            });
                            
                            mui('#pullrefresh')
                            .pullRefresh()
                            .endPullupToRefresh((itemList.length == data.maxCount)); //refresh completed
                            
                            
                            var list = itemList.map(function (data) {
                                return (
                                    <System key={data.id} data={data} user={user}/>
                                )
                            });
                        }
                        
                        me.setState({list: list});
                        
                        
                    }
                    else {
                        alert(data.desc);
                    }
                }.bind(me)
            }, lf.finger.api.message.sysMsg);
        },
        componentWillReceiveProps: function (nextProps) {
            var page = nextProps.page;
            this.getHouseNum(nextProps.user, page)
        },
        render: function () {
            return (
                <div className="property-msg">
                    <div className="page" ref="defaultPage">
                        {this.state.list}
                    </div>
                </div>
            );
        }
    });
    
    const PullRefreshCon = RC({
        getInitialState() {
            return {
                page: 1
            }
        },
        pullupFn: function () {
            let newPage = ++this.state.page;
            this.setState({page: newPage});
        },
        render() {
            var me = this;
            return <PullRefresh pullupFn={me.pullupFn} data={<Body page={me.state.page} user={me.props.user}/>}/>;
        }
    });
    
    lf.appJs.getUser(function (user) {
        r.render(
            <PullRefreshCon user={user}/>,
            document.getElementById('root'), function () {
                $('#root').css('background', 'rgb(239, 239, 239)');
                $('.mui-scroll').css({'background': 'rgb(239, 239, 239)'});
                
            }
        )
    })
    
    
})(liefeng, React)
